package com.sky.service.impl;

import com.sky.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class ShopServiceImpl implements ShopService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    public static final String KEY = "shopStatus";

    @Override
    public void changeStatus(Integer status) {
        stringRedisTemplate.opsForValue().set(KEY, status.toString());
    }

    @Override
    public Integer getShopStatus() {
        String s = null;
        try {
            s = stringRedisTemplate.opsForValue().get(KEY);
        } catch (Exception e) {
            log.error("Redis服务异常！");
            throw new RuntimeException(e);
        }
        if (s != null) {
            return Integer.parseInt(s);
        }
        stringRedisTemplate.opsForValue().set(KEY, "1");
        return 1;
    }
}
